package com.open.capacity.finance.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.open.capacity.exception.BusinessException;
import com.open.capacity.finance.entity.SourceBaseUsecar;
import com.open.capacity.finance.util.FileUtil;
import com.open.capacity.user.model.SysCompany;
import com.open.capacity.user.model.SysFile;
import com.open.capacity.user.service.SysCompanyService;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.SourceBasePay;
import com.open.capacity.finance.dao.SourceBasePayDao;
import com.open.capacity.finance.service.SourceBasePayService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class SourceBasePayServiceImpl implements SourceBasePayService {

    @Autowired
    private SourceBasePayDao sourceBasePayDao;

    @Autowired
    private SysCompanyService companyService;

    /**
     * 保存到数据库
     *
     * @param fileList
     */
    @Transactional
    public int save2db(String companyId, Integer kjnd, Integer importway, List<SysFile> fileList) {
        //删除旧数据
        if (importway == 1) {
            delete(companyId, kjnd);
        }
        for (SysFile file : fileList) {
            MultipartFile multipartFile = FileUtil.toMultipartFile("paydataXls", file.getUrl());
            ImportParams params = new ImportParams();
            params.setTitleRows(3);
            params.setHeadRows(1);
            // 开启excel校验
            params.setNeedVerify(false);

            List<SourceBasePay> list = new ArrayList<>();
            try {
                list = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                        SourceBasePay.class, params);
            } catch (Exception e) {
                log.error("从文件读取数据错误:{}", e.getMessage());
            }

            SysCompany company = companyService.findById(companyId);
            for (SourceBasePay item : list) {
                if (StringUtil.isEmpty(item.getSkrName())) {
                    continue;
                }
                item.setCompanyId(companyId);
                item.setCompanyName(company.getFullName());
                item.setKjnd(kjnd);
                sourceBasePayDao.save(item);
            }
        }
        return 1;
    }


    /**
     * 删除
     *
     * @param companyId
     */
    public int delete(String companyId, Integer kjnd) {
        return sourceBasePayDao.deleteByCompanyIdAndKjnd(companyId, kjnd);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SourceBasePay findById(String id) {
        return sourceBasePayDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SourceBasePay> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SourceBasePay> list = sourceBasePayDao.findAll(params);
        PageInfo<SourceBasePay> pageInfo = new PageInfo(list);

        return PageResult.<SourceBasePay>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SourceBasePay> findAll(Map<String, Object> params) {
        if (params.containsKey("pzrq")) {
            String pzrq = params.get("pzrq").toString();
            String pzrqStart = pzrq + " 00:00:00";
            String pzrqEnd = pzrq + " 23:59:59";

            params.put("pzrqStart", pzrqStart);
            params.put("pzrqEnd", pzrqEnd);
        }
        return sourceBasePayDao.findAll(params);
    }

}
